home *** CD-ROM | disk | FTP | other *** search
/ AMIGA-CD 2 / Amiga-CD - Volume 2.iso / ungepackte_daten / 1993 / 2 / 02 / suchalgorithmen / listing4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-01  |  918 b   |  37 lines

  1. struct Knoten {
  2.   long Schluessel, Info;
  3.   struct Knoten *Left,*Right;
  4. };
  5. struct Knoten *Kopf, *Ende;
  6. void Init()
  7. {
  8.   Kopf=(struct Knoten *)malloc(sizeof(struct Knoten));
  9.   Ende=(struct Knoten *)malloc(sizeof(struct Knoten));
  10.   Kopf->Right = Ende; Kopf->Schluessel=0;
  11.   Ende->Left=Ende; Ende->Right=Ende; Ende->Info = -1;
  12. }
  13. void Einfuegen( long schluessel, long info)
  14. {
  15.   struct Knoten *a,*b:
  16.   a=Kopf; b=Kopf->Right;
  17.   while( b != Ende ) {
  18.     a=b; b=(schluessel < b->Schluessel) ? b->Left : b->Right;
  19.   }
  20.   b=(struct Knoten *)malloc(sizeof(struct Knoten));
  21.   b->Schluessel=schluessel; b->Info=info; b->Left=Ende;
  22.   b->Right=Ende;
  23.   if( schluessel < a->Schluessel )
  24.     a->Left=b;
  25.   else
  26.     a->Right=b;
  27. }
  28. long Suchen(long schluessel)
  29. {
  30.   struct Knoten *a=Kopf->Right;
  31.   Ende->Schluessel=schluessel;
  32.   while( schluessel != a->Schluessel )
  33.     a=(schluessel < a->Schluessel) ? a->Left : a->Right;
  34.   return a->Info;
  35. }
  36.  
  37.